﻿using System;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using LitJson;

public class UIManager : MonoBehaviour
{
    private static UIManager instance;

    public static UIManager GetInstance()
    {
        return instance;
    }

    void Awake()
    {
        instance = this;
    }

    public Dictionary<string, Dictionary<string, List<string>>> datas;
    public City CurrentCityData;
    private UIView _UIView;

    void Start()
    {
        Init();
        foreach (var VARIABLE in datas)
        {
            print(VARIABLE.Key);
        }
        //print(datas.Count);
    }

    void Update()
    {
        //print(datas.Count);
        //print(CurrentCityData.city);
    }
    void Init()
    {
        datas = new Dictionary<string, Dictionary<string, List<string>>>();
        _UIView = GameObject.FindWithTag("UIView").GetComponent<UIView>();
        CurrentCityData = new City("0","12","123","1234");
        //string url = NetworkConfig.RequestURL + NetworkConfig.Key;
        string url = "file://" + Application.streamingAssetsPath + "/City.txt";
        //string url = "file://" + Application.streamingAssetsPath + "/Citytest.txt";
        NetworkKit.GetInstance().Requst(url,
            (string txt) =>
            {
                JsonData data = JsonMapper.ToObject(txt);
                JsonData result = data["result"];
                //遍历每个元素
                for (int i = 0; i < result.Count; i++)
                {
                    City city = JsonMapper.ToObject<City>(result[i].ToJson());
                    InsertDate(city);
                    //print(city.id);
                }
            },
            null);
    }

    //插入数据
    void InsertDate(City cityModel)
    {
        if (!datas.ContainsKey(cityModel.province))
        {
            datas.Add(cityModel.province, new Dictionary<string, List<string>>());
            datas[cityModel.province].Add(cityModel.city, new List<string>()
            {
                cityModel.district
            });
        }
        else
        {
            if (!datas[cityModel.province].ContainsKey(cityModel.city))
            {
                datas[cityModel.province].Add(cityModel.city, new List<string>()
                {
                    cityModel.district
                });
            }
            else
            {
                datas[cityModel.province][cityModel.city].Add(cityModel.district);
            }
        }
    }

    public void RefrashCurrentCityData(string value, string changeField)
    {
        Type type = CurrentCityData.GetType();
        PropertyInfo fieldInfo = type.GetProperty(changeField);
        fieldInfo.SetValue(CurrentCityData, value, null);
        switch (changeField)
        {
            case "province":
                CurrentCityData.city = null;
                CurrentCityData.district = null;
                break;
            case "city":
                CurrentCityData.district = null;
                break;
            //default:
            //    break;
        }
        _UIView.RefreshCityInfo(CurrentCityData);
    }
}
